package com.zl.customer.web;

import com.zl.customer.pojo.Userinfo;
import com.zl.customer.service.UserinfoService;
import com.zl.customer.service.impl.UserinfoServiceImpl;
import com.zl.customer.web.util.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

/**
 * @author suke
 * @create 2021-07-06 9:58
 */
@WebServlet(name = "LoginServlet" ,urlPatterns = "/login.action")
public class LoginServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.设置编码
        //post请求编码
        request.setCharacterEncoding("UTF-8");
        //响应编码
        response.setContentType("text/html;charset=UTF-8");
        //2.获取请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //3.数据转换

        //4.业务逻辑代码
        /*UserinfoDao userinfoDao = new UserinfoDaoImpl();

        Userinfo userinfo = userinfoDao.queryByNameAndPass(username,password);*/
        //调用业务层
        //UserinfoService  userinfoService = new UserinfoServiceImpl();

        Userinfo  userinfo = SpringUtils.getServiceBean(UserinfoService.class,this.getServletContext()).login(username,password);
        if(userinfo == null){  //登录失败
            // "用户名或密码错误"
            // 域对象
            request.setAttribute("errorMsg","用户名或密码错误");//安全性
            //5.页面跳转
            request.getRequestDispatcher("/a.html").forward(request,response);


        }else{  //登录成功
            //是否记住密码
            //获取记住密码的checkbox的值,如果有,表示用户勾选, 把用户名,密码 保存到两个cookie中  响应给客户端
            String remenber  =  request.getParameter("remenber");
            if("rmb".equals(remenber)){ //勾选  //避免出现空指针异常
                //创建cookie
                Cookie  nameCookie = new  Cookie("name",username);
                Cookie  passCookie = new  Cookie("pass",password);

                //Cookie的默认存活时间: -1   7天有效
                nameCookie.setMaxAge(7*24*60*60);
                passCookie.setMaxAge(7*24*60*60);

                //保存到客户端
                response.addCookie(nameCookie);
                response.addCookie(passCookie);
            }else{//没有勾选 不记住密码,  清除cookie
                Cookie[] cookies =  request.getCookies();
                if(cookies!=null && cookies.length > 0){
                    for(Cookie cookie : cookies){
                        if(cookie.getName().equals("name") || cookie.getName().equals("pass")){
                            //删除cookie
                            cookie.setMaxAge(0);

                            //保存给客户端
                            response.addCookie(cookie);
                        }
                    }
                }
            }

          //把用户信息保存到Session域中
            //1.得到Session对象
            HttpSession session = request.getSession();
            //2.把用户信息保存到session域
            session.setAttribute("loginUser",userinfo);
            Object jumpUrl = session.getAttribute("jumpUrl");
           // if(jumpUrl != null){
              //  response.sendRedirect(jumpUrl.toString());
                //删除session中jumpUrl属性  removeAttribute()
              //  session.removeAttribute("jumpUrl");
            //}else{
             //   response.sendRedirect(request.getContextPath()+"/jsp/index.jsp");
           // }
            response.sendRedirect(request.getContextPath()+"/jsp/index.jsp");

        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
